package com.action.admin.website;

import java.util.Date;
import java.util.List;

import com.action.BaseAction;
import com.dao.ArticleDao;
import com.entity.Article;
import com.util.Page;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

/**
 * 文章Article
 * @author Administrator
 * 
 */
// @ParentPackage("json-default")
@ParentPackage("extendsjson-default")
@Namespace("/website")
@ResultPath("/WEB-INF/pages")
public class ArticleAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ArticleAction.class);

	/**
	 * 
	 * @return
	 */
	@Action(value = "addArticle", results = { @Result(name = "success", location = "kindeditor.jsp") })
	public String addArticle() {
		return SUCCESS;
	}
	
	
	/**
	 * 
	 * @return
	 */
	@Action(value = "saveArticle", results = { @Result(name = "success",  location = "kindeditor.jsp") })
	public String saveArticle() {
		try {
			if(article != null)
				article.setCreatedate(new Date());
			boolean b = articleDao.saveArticle(article);
//			if(b){
//				this.outString("0");
//			}else{
//				this.outString("1");
//			}
			this.setRequest("content1",article.getContent());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 */
	@Action(value = "updateArticle", results = { @Result(name = "success", type = "json") })
	public String updateArticle() {
		try {
			Article article = articleDao.findArticleById(id);
			articleDao.updateArticle(article);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 */
	@Action(value = "findArticle", results = { @Result(name = "success", location = "/top.jsp") })
	public String findArticle() {
		try {
			article = articleDao.findArticleById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 */
	@Action(value = "findArticlelist", results = { @Result(name = "success", location = "/top.jsp") })
	public String findArticlelist() {
		try {
			list = articleDao.findArticleList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 */
	@Action(value = "findArticleforpage", results = { @Result(name = "success", location = "/top.jsp") })
	public String findArticlepage() {
		try {
			page = articleDao.findbyPage(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	private ArticleDao articleDao = (ArticleDao) ioc.newInstance(ArticleDao.class);
	private Article article;
	private String id;
	boolean flag = false;
	private String content;
	private List list;
	private Page page;

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}


	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

}
